package admin;

import Dao.communityDao;
import Dao.communityDaoImpl;
import been.Health;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Date;

@WebServlet(value = "/addHealthInformation")
public class addHealthInformationServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        communityDao dao = new communityDaoImpl();
        String message=null;
        Health health=null;

        int health_id=0;
        String health_idParam=request.getParameter("health_id");
        if(health_idParam==null){
            message="id不能为空";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/WEB-INF/admin/addHealthInformation.jsp").forward(request,response);
        }
        else
        {
            try{
                health_id=Integer.parseInt(health_idParam);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        String health_name=request.getParameter("health_name");
        if(health_name==null){
            message="名字不能为空";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/WEB-INF/admin/addHealthInformation.jsp").forward(request,response);
            return;
        }

        String gender=request.getParameter("gender");
        if(gender==null){
            message="性别不能为空";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/WEB-INF/admin/addHealthInformation.jsp").forward(request,response);
            return;
        }

        int age=0;
        String ageParam=request.getParameter("age");
        if(ageParam==null){
            message="年龄不能为空";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/WEB-INF/admin/addHealthInformation.jsp").forward(request,response);
            return;
        }
        else
        {
            try{
                age=Integer.parseInt(ageParam);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        String chronic_diseases=request.getParameter("chronic_diseases");
        String allergies=request.getParameter("allergies");
        Date last_updated=new Date();
        try{
            health=new Health(health_id,health_name,gender,age,chronic_diseases,allergies,last_updated);
            int res=dao.addHealth(health);
            if(res>0){
                message="登记成功";
                request.setAttribute("message",message);
                request.getRequestDispatcher("/WEB-INF/admin/addHealthInformation.jsp").forward(request,response);
            }
            else
            {
                message="登记失败";
                request.setAttribute("message",message);
                request.getRequestDispatcher("/WEB-INF/admin/addHealthInformation.jsp").forward(request,response);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
